// URL参数解析
export function getUrlParam(name) {
    const params = new URLSearchParams(window.location.search);
    return params.get(name);
}

// 显示加载状态
export function showLoading(text = '加载中...') {
    const loading = document.querySelector('.loading');
    loading.textContent = text;
    loading.style.display = 'block';
}

// 隐藏加载状态
export function hideLoading() {
    document.querySelector('.loading').style.display = 'none';
}

// 显示弹窗
export function showDialog() {
    const dialog = document.getElementById('dialogMask');
    dialog.style.display = 'block';
    
    document.getElementById('saveBtn').onclick = saveImage;
    document.getElementById('cancelBtn').onclick = () => {
        dialog.style.display = 'none';
    };
}

// 保存图片
export function saveImage() {
    const gifUrl = document.querySelector('#gif-container img').src;
    
    if (typeof wx !== 'undefined') {
        wx.downloadImage({
            url: gifUrl,
            isShowProgressTips: 0,
            success: (res) => {
                wx.saveImageToPhotosAlbum({
                    filePath: res.tempFilePath,
                    success: () => alert('已保存到相册'),
                    fail: (err) => alert('保存失败: ' + JSON.stringify(err))
                });
            },
            fail: (err) => alert('下载失败: ' + JSON.stringify(err))
        });
    } else {
        // 非微信环境处理
        const link = document.createElement('a');
        link.href = gifUrl;
        link.download = 'image.gif';
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
    }
    
    document.getElementById('dialogMask').style.display = 'none';
}